package com.lifeverse.factory.type;

import com.lifeverse.entity.LifeEntity;
import com.lifeverse.entity.enums.LifeEntityType;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.util.Map;

/**
 * 企业生命体创建器
 * 专门创建企业类型的生命体
 */
@Slf4j
@Component
public class CorporateLifeEntityCreator implements LifeEntityCreator {
    
    @Override
    public LifeEntity create(String name, String description, Map<String, Object> configuration) {
        log.info("Creating corporate life entity: {}", name);
        
        LifeEntity entity = new LifeEntity();
        entity.setType(LifeEntityType.CORPORATE);
        
        // 设置企业生命体特有的初始属性
        setupCorporateAttributes(entity, configuration);
        
        return entity;
    }
    
    @Override
    public LifeEntity clone(LifeEntity originalEntity, String newName, double variationLevel) {
        log.info("Cloning corporate life entity: {} to {}", originalEntity.getName(), newName);
        
        LifeEntity clonedEntity = new LifeEntity();
        clonedEntity.setType(LifeEntityType.CORPORATE);
        
        // 复制原始企业的属性
        copyCorporateAttributes(clonedEntity, originalEntity);
        
        return clonedEntity;
    }
    
    @Override
    public boolean validateConfiguration(Map<String, Object> configuration) {
        if (configuration == null) {
            return true; // 允许空配置，使用默认值
        }
        
        // 验证企业特有配置
        if (configuration.containsKey("industry")) {
            String industry = (String) configuration.get("industry");
            if (industry == null || industry.trim().isEmpty()) {
                return false;
            }
        }
        
        if (configuration.containsKey("employeeCount")) {
            Object employeeCount = configuration.get("employeeCount");
            if (!(employeeCount instanceof Number) || ((Number) employeeCount).intValue() < 0) {
                return false;
            }
        }
        
        if (configuration.containsKey("revenue")) {
            Object revenue = configuration.get("revenue");
            if (!(revenue instanceof Number) || ((Number) revenue).doubleValue() < 0) {
                return false;
            }
        }
        
        return true;
    }
    
    @Override
    public Map<String, Object> getDefaultConfiguration() {
        return Map.of(
                "industry", "Technology",
                "employeeCount", 100,
                "revenue", 1000000.0,
                "marketPosition", "Emerging",
                "innovationLevel", "Medium",
                "sustainabilityScore", 50.0,
                "digitalMaturity", "Intermediate",
                "stakeholderEngagement", "Active"
        );
    }
    
    /**
     * 设置企业生命体特有属性
     */
    private void setupCorporateAttributes(LifeEntity entity, Map<String, Object> configuration) {
        Map<String, Object> config = configuration != null ? configuration : getDefaultConfiguration();
        
        // 根据行业类型设置不同的初始能力值
        String industry = (String) config.getOrDefault("industry", "Technology");
        setupIndustrySpecificAttributes(entity, industry);
        
        // 根据员工数量调整协作能力
        int employeeCount = ((Number) config.getOrDefault("employeeCount", 100)).intValue();
        adjustCollaborationBySize(entity, employeeCount);
        
        // 根据收入调整资源管理能力
        double revenue = ((Number) config.getOrDefault("revenue", 1000000.0)).doubleValue();
        adjustResourceManagementByRevenue(entity, revenue);
        
        // 设置企业特有的创造力和智慧基线
        entity.setCreativityScore(BigDecimal.valueOf(60.0)); // 企业通常有中等偏上的创造力
        entity.setWisdomScore(BigDecimal.valueOf(70.0)); // 企业积累的经验智慧
    }
    
    /**
     * 根据行业设置特定属性
     */
    private void setupIndustrySpecificAttributes(LifeEntity entity, String industry) {
        switch (industry.toLowerCase()) {
            case "technology" -> {
                entity.setCreativityScore(BigDecimal.valueOf(80.0));
                entity.setWisdomScore(BigDecimal.valueOf(75.0));
                entity.setCollaborationScore(BigDecimal.valueOf(70.0));
            }
            case "manufacturing" -> {
                entity.setCreativityScore(BigDecimal.valueOf(50.0));
                entity.setWisdomScore(BigDecimal.valueOf(85.0));
                entity.setCollaborationScore(BigDecimal.valueOf(80.0));
            }
            case "finance" -> {
                entity.setCreativityScore(BigDecimal.valueOf(60.0));
                entity.setWisdomScore(BigDecimal.valueOf(90.0));
                entity.setCollaborationScore(BigDecimal.valueOf(65.0));
            }
            case "healthcare" -> {
                entity.setCreativityScore(BigDecimal.valueOf(70.0));
                entity.setWisdomScore(BigDecimal.valueOf(95.0));
                entity.setCollaborationScore(BigDecimal.valueOf(85.0));
            }
            case "education" -> {
                entity.setCreativityScore(BigDecimal.valueOf(75.0));
                entity.setWisdomScore(BigDecimal.valueOf(90.0));
                entity.setCollaborationScore(BigDecimal.valueOf(90.0));
            }
            default -> {
                entity.setCreativityScore(BigDecimal.valueOf(60.0));
                entity.setWisdomScore(BigDecimal.valueOf(70.0));
                entity.setCollaborationScore(BigDecimal.valueOf(65.0));
            }
        }
    }
    
    /**
     * 根据企业规模调整协作能力
     */
    private void adjustCollaborationBySize(LifeEntity entity, int employeeCount) {
        BigDecimal currentCollaboration = entity.getCollaborationScore();
        
        if (employeeCount < 50) {
            // 小企业：协作更灵活但规模有限
            entity.setCollaborationScore(currentCollaboration.multiply(BigDecimal.valueOf(0.9)));
        } else if (employeeCount < 500) {
            // 中型企业：协作能力适中
            entity.setCollaborationScore(currentCollaboration);
        } else if (employeeCount < 5000) {
            // 大型企业：协作能力强但可能有官僚化
            entity.setCollaborationScore(currentCollaboration.multiply(BigDecimal.valueOf(1.1)));
        } else {
            // 超大型企业：协作复杂但资源丰富
            entity.setCollaborationScore(currentCollaboration.multiply(BigDecimal.valueOf(1.2)));
        }
    }
    
    /**
     * 根据收入调整资源管理能力（通过智慧分数体现）
     */
    private void adjustResourceManagementByRevenue(LifeEntity entity, double revenue) {
        BigDecimal currentWisdom = entity.getWisdomScore();
        
        if (revenue < 1000000) {
            // 小收入：资源管理经验有限
            entity.setWisdomScore(currentWisdom.multiply(BigDecimal.valueOf(0.8)));
        } else if (revenue < 10000000) {
            // 中等收入：有一定资源管理经验
            entity.setWisdomScore(currentWisdom);
        } else if (revenue < 100000000) {
            // 高收入：丰富的资源管理经验
            entity.setWisdomScore(currentWisdom.multiply(BigDecimal.valueOf(1.2)));
        } else {
            // 超高收入：顶级资源管理能力
            entity.setWisdomScore(currentWisdom.multiply(BigDecimal.valueOf(1.4)));
        }
    }
    
    /**
     * 复制企业属性
     */
    private void copyCorporateAttributes(LifeEntity clonedEntity, LifeEntity originalEntity) {
        // 复制基本属性
        clonedEntity.setWisdomScore(originalEntity.getWisdomScore());
        clonedEntity.setCreativityScore(originalEntity.getCreativityScore());
        clonedEntity.setCollaborationScore(originalEntity.getCollaborationScore());
        clonedEntity.setEnergyLevel(originalEntity.getEnergyLevel());
        clonedEntity.setHealthScore(originalEntity.getHealthScore());
        
        // 复制配置信息
        clonedEntity.setConfiguration(originalEntity.getConfiguration());
    }
}